package com.frame.work.frame.deal.device0fbe;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.frame.work.common.constant.TypeEnum;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.config.SpringUtil;
import com.frame.work.entity.FingerprintLockMessageDO;
import com.frame.work.entity.TblFingerprint;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.frame.deal.util.WeakCurrentDeviceUtil;
import com.frame.work.mapper.SelectDeviceMapper;
import com.frame.work.mapper.TblFingerprintMapper;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

import static com.frame.work.common.constant.CallType.TYPE_CONTROL;
import static com.frame.work.common.constant.CallType.TYPE_UP;
import static java.lang.Integer.parseInt;

/**
 * 指纹锁 聪明屋
 *
 * @author wjming
 */
@Data
public class ZhiWenSuoCongMingWu0Fbe0202 extends BaseFrame {

    public ZhiWenSuoCongMingWu0Fbe0202(String gatewayMac, String data, String type) {

        //将基础数据进行处理
        super(gatewayMac, data);
        //根据不同类型进行判断
        if (TYPE_CONTROL.equals(type)) {
            control(gatewayMac, data);
        } else if (TYPE_UP.equals(type)) {
            up(gatewayMac, data);
        }


    }

    public void up(String gatewayMac, String data) {
        //处理此设备特有的数据

        String reportType = StringUtils.substring(data, 40, 42);
        //将actions信息放入json对象中
        Map<String, Object> actions = new HashMap();
        actions.put("reportType", reportType);
        this.setActions(actions);
        //处理好的json对象
        String json = JSONObject.toJSONString(this);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, json);

    }

    public void control(String gatewayMac, String data) {
        final String BY_APP = "A153";
        final String BY_PSD = "A151";
        final String BY_FINGERPRINT = "A150";
        final String BY_CARD = "A152";
        final String IGNORE_TYPE = "A100";
        //将基础数据进行处理

        this.setDeviceType(getDeviceType(data));
        this.setProductType(getProdType(data));

        String content = "";
        String eventType = getEventType(data);
        String userId = getFingerprintId(data);

        //获取mapper进行数据库操作
        SelectDeviceMapper testMapper = SpringUtil.getBean(SelectDeviceMapper.class);

        FingerprintLockMessageDO lockMessage = testMapper.getFingerprintLockMessage(userId, this.getDeviceMac());
        Boolean isSend = true;
        if (lockMessage != null) {
            switch (eventType) {
                //忽略类型
                case IGNORE_TYPE:
                    isSend = false;
                    break;
                //通过App打开
                case BY_APP:
                    content = "通过APP远程打开";
                    break;
                //通过密码打开
                case BY_PSD:
                    content = "通过密码打开";
                    break;
                //通过指纹锁打开
                case BY_FINGERPRINT:
                    String idType = parseInt(userId, 16) > 5 ? "02" : "01";
                    if (StringUtils.isEmpty(lockMessage.getFingerprintName())) {
                        //指纹锁表里新增数据
                        TblFingerprint tblFingerprint = TblFingerprint.builder()
                                .deviceId(lockMessage.getDeviceId())
                                .number(Integer.toString(parseInt(userId, 16)))
                                .name("指纹" + parseInt(userId, 16))
                                .idType(idType)
                                .build();
                        TblFingerprintMapper tblFingerprintMapper = SpringUtil.getBean(TblFingerprintMapper.class);
                        tblFingerprintMapper.insert(tblFingerprint);

                        content = "被指纹" + parseInt(userId, 16) + "打开";
                    } else {
                        content = "被" + lockMessage.getFingerprintName() + "打开";
                    }

                    TblFingerprintMapper tblFingerprintMapper = SpringUtil.getBean(TblFingerprintMapper.class);
                    TblFingerprint fingerprint = tblFingerprintMapper.selectOne(Wrappers.<TblFingerprint>lambdaQuery()
                            .eq(TblFingerprint::getDeviceId, lockMessage.getDeviceId())
                            .eq(TblFingerprint::getNumber, userId).eq(TblFingerprint::getIdType, idType));
                    // 场景触发
                    WeakCurrentDeviceUtil.sceneTrigger(fingerprint);
                    break;
                //通过卡打开
                case BY_CARD:
                    content = "通过门卡打开";
                    break;
                default:
                    break;
            }
            if (isSend) {
                String type = TypeEnum.NORMAL.code();
                WeakCurrentDeviceUtil.SaveLogAndSendJpush(this.getDeviceMac(), this.getGatewayMac(), content, type);
            }
        }


        //处理好的json对象
        String json = JSONObject.toJSONString(this);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, json);
    }


    /**
     * 特殊：获取设备类型，一般情况下基类中会自己去截取
     *
     * @param data
     * @return
     */
    private static String getDeviceType(String data) {
        return StringUtils.substring(data, 22, 24);
    }

    /**
     * 特殊：获取产品类型类型，一般情况下基类中会自己去截取
     *
     * @param data
     * @return
     */
    private static String getProdType(String data) {
        return StringUtils.substring(data, 24, 26);
    }

    /**
     * 获取开门的事件类型
     *
     * @param data
     * @return
     */
    private static String getEventType(String data) {
        return StringUtils.substring(data, 36, 40);
    }

    /**
     * 获取指纹ID
     *
     * @param data
     * @return
     */
    private static String getFingerprintId(String data) {
        String userId = StringUtils.substring(data, 42, 46);
        /**
         *16->10
         */
        userId = Integer.toString(parseInt(userId, 16));
        return userId;
    }

}
